package com.shaozi.nc.execpool;

import java.lang.reflect.InvocationTargetException;

import org.apache.log4j.Logger;

import com.shaozi.lib.execpool.base.ExecutorDataBean;
import com.shaozi.lib.throwable.SzException;
import com.shaozi.nc.bean.resp.ReqMakeSecretaryReadByIdsBean;
import com.shaozi.nc.bean.resp.RespMessageSetReadForTypeBean;
import com.shaozi.nc.data.bean.CompanyUserBean;
import com.shaozi.nc.data.bean.req.ReqMakeSecretaryReadBean;
import com.shaozi.nc.data.model.SecretaryModel;
import com.shaozi.nc.io.mq.MakeSecretaryReadProducer;
import com.shaozi.nc.manage.secretary.BaseSecretaryManage;

public abstract class BaseMakeSecretaryReadMQRunnable {

	private Logger logger = Logger.getLogger(BaseMakeSecretaryReadMQRunnable.class);

	public BaseMakeSecretaryReadMQRunnable() {

	}

	protected void run(ExecutorDataBean bean, MakeSecretaryReadProducer readProducer, BaseSecretaryManage manage) {
		RespMessageSetReadForTypeBean respBean = (RespMessageSetReadForTypeBean) bean.datas.get("respBean");
		CompanyUserBean companyUserBean = (CompanyUserBean) bean.datas.get("companyUserBean");
		ReqMakeSecretaryReadBean reqBean = new ReqMakeSecretaryReadBean();
		try {
			org.apache.commons.beanutils.BeanUtils.copyProperties(reqBean, companyUserBean);
			reqBean.setUpdateTime(respBean.getUpdateTime());
			if (respBean.getCount() == null || respBean.getCount() == 0) {
				logger.warn("已读数量为空不发送到oa中，updateTime:" + respBean.getUpdateTime() + ",companyId:"
						+ companyUserBean.getCompanyId() + ",userId:" + companyUserBean.getUserId() + ",modelType:"
						+ reqBean.getModuleType());
				return;
			}
			if (companyUserBean instanceof ReqMakeSecretaryReadByIdsBean) {
				ReqMakeSecretaryReadByIdsBean req = (ReqMakeSecretaryReadByIdsBean) companyUserBean;
				String[] ids = req.getId();
				if ((ids == null) || (ids.length == 0)){
					logger.warn("ids为空不发送");
					return;
				}
				SecretaryModel model = manage.getById(companyUserBean.getCompanyId(), ids[0]);
				if (model == null){
					logger.warn("小秘书model查询为空不发送");
					return;
				}
				reqBean.setModuleType(model.getType());
				reqBean.setNotifyType(model.getNotifyType());
				reqBean.setSourceType(model.getSourceType());
			}
			readProducer.pushData(reqBean);
		} catch (IllegalAccessException | InvocationTargetException |

				SzException e) {
			logger.error(SzException.getStackMessage(e));
		}
	}
}
